package com.gobygang.server;
import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.gobygang.shared.ContactInfo;
import com.gobygang.shared.LoginInfo;
import com.gobygang.shared.TaxiGangInfo;

public class Mailer {

	private static Mailer instance = new Mailer();
	public static Mailer getInstance(){return instance;}
	
	
	public static void search(TaxiGangInfo info){
		String message = "SEARCH" ;
		send("hall@hall-services.de",message, info);
	}
	


	public static void sendMessage(String message, TaxiGangInfo info, LoginInfo loginInfo){
		//Log.info("Mailer:sendMessage locale/country: "+ loginInfo.getLocale()+"/"+loginInfo.getCountry() );

		send("hall@hall-services.de",message, info);
		//if ("ro".equals(loginInfo.getLocale())){
			//send("eduard.encuna@gmail.com",message, info);
		//}
	}
	
	private static void send(String receiverEmail,String message, TaxiGangInfo info){
		//Log.info("Mailer:"+receiverEmail+" : "+message);

        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        String msgBody = "From:"+info.getStart()+"\n";
        msgBody += "To:"+info.getDestination()+"\n";
        msgBody += "Time:"+info.getLocalDepartureTime()+"\n";
        msgBody += info.getLink();

        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("peterredmondhall@gmail.com", "Taxigang"));
            msg.addRecipient(Message.RecipientType.TO,
                             new InternetAddress(receiverEmail, "Info"));
            msg.setSubject(message);
            msg.setText(msgBody);
            Transport.send(msg);
    
        } catch (AddressException e) {
            // ...
        } catch (MessagingException e) {
            // ...
	    } catch (UnsupportedEncodingException e) {
	        // ...
	    }

	}
	

	public void sendContactInfo(ContactInfo info){

        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        String msgBody = "Name:"+info.getName()+"\n";
        msgBody += "Email:"+info.getEmail()+"\n";
        msgBody += "Handy:"+info.getMessage()+"\n";

        //Log.info("Sending contact info");

        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("peterredmondhall@gmail.com", "Taxigang"));
            msg.addRecipient(Message.RecipientType.TO,
                             new InternetAddress("info@taxigang.com", "Info"));
            msg.setSubject("A contact message was received " );
            msg.setText(msgBody);
            Transport.send(msg);
    
            //and now from info@taxigang.com
            msg.setFrom(new InternetAddress("info@taxigang.com", "Taxigang"));
            Transport.send(msg);

        } catch (AddressException e) {
            //Log.info(e.getMessage());
        } catch (MessagingException e) {
            //Log.info(e.getMessage());
	    } catch (UnsupportedEncodingException e) {
            //Log.info(e.getMessage());
	    }

	}


}
